package com.asktgapp.user.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.asktgapp.JGTool.JGColor;
import com.asktgapp.JGTool.Utils;
import com.asktgapp.MyConstant;
import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.model.FreeAskSearchVO;
import com.asktgapp.model.HomeServiceVO;
import com.asktgapp.model.MyPublishNewVO;
import com.asktgapp.model.MyPublishVO;
import com.asktgapp.model.RentInfoVO;
import com.asktgapp.model.UsedMachineVO;
import com.asktgapp.modulebase.common.adapter.BaseAdapter;
import com.asktgapp.modulebase.common.adapter.BaseViewHolder;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.modulebase.common.util.Util;
import com.asktgapp.user.activity.EditRentActicity;
import com.asktgapp.user.activity.EditUsedMachineActivity;
import com.asktgapp.user.activity.FreeAskAnswerDetailActivity;
import com.asktgapp.user.activity.HomeServiceDetailActivity;
import com.asktgapp.user.activity.PhotoViewActivity;
import com.asktgapp.user.activity.RentDetailActivity;
import com.asktgapp.user.activity.UsedMachineDetailActivity;
import com.asktgapp.utils.ImageDisplayUtil;
import com.yanzhenjie.recyclerview.swipe.SwipeMenu;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItem;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuItemClickListener;
import com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMoveListener;
import com.yanzhenjie.recyclerview.swipe.touch.OnItemMovementListener;
import com.yanzhenjie.recyclerview.swipe.widget.DefaultItemDecoration;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 新我的发布子页面
 */

public class MyPublishItemNewFragment extends BaseFragment {
    private SwipeMenuRecyclerView mRecyclerView;
    private BaseAdapter mBaseAdapter;
    private int type;
    private static final int VIEW_TYPE_USED = 1;//二手
    private static final int VIEW_TYPE_RENT = 2;//出租
    private static final int VIEW_TYPE_HOME_SERVICE = 7;//上门服务
    private static final int VIEW_TYPE_ASK = 8;//快速问答
    private Apiservice mApiservice;

    public static MyPublishItemNewFragment newInstance(int type) {
        MyPublishItemNewFragment fragment = new MyPublishItemNewFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_my_publish_item, container, false);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }

    @Override
    protected void initView(View rootView) {
        Bundle bundle = getArguments();
        type = bundle.getInt("type", MyConstant.TYPE_FREE_ASK);
        mRecyclerView = rootView.findViewById(R.id.recycle);
        mRecyclerView.addItemDecoration(new DefaultItemDecoration(JGColor.APP_BACKGROUND));
//        mRecyclerView.setItemViewSwipeEnabled(true);
        /**
         * 菜单创建器，在Item要创建菜单的时候调用。
         */
        SwipeMenuCreator swipeMenuCreator = new SwipeMenuCreator() {
            @Override
            public void onCreateMenu(SwipeMenu swipeLeftMenu, SwipeMenu swipeRightMenu, int viewType) {
                int width = getResources().getDimensionPixelSize(R.dimen.view_height_80);

                // 1. MATCH_PARENT 自适应高度，保持和Item一样高;
                // 2. 指定具体的高，比如80;
                // 3. WRAP_CONTENT，自身高度，不推荐;
                int height = ViewGroup.LayoutParams.MATCH_PARENT;
                SwipeMenuItem deleteItem = new SwipeMenuItem(getActivity())
                        .setBackgroundColor(Color.parseColor("#ff5c5d"))
                        .setText("删除")
                        .setTextColor(Color.WHITE)
                        .setWidth(width)
                        .setHeight(height);
                swipeRightMenu.addMenuItem(deleteItem);// 添加菜单到右侧。
            }
        };

//        mRecyclerView.setOnItemMovementListener(mItemMovementListener);
//        mRecyclerView.setOnItemMoveListener(onItemMoveListener);

        SwipeMenuItemClickListener mMenuItemClickListener = new SwipeMenuItemClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge) {
                menuBridge.closeMenu();
                int direction = menuBridge.getDirection(); // 左侧还是右侧菜单。
                final int adapterPosition = menuBridge.getAdapterPosition(); // RecyclerView的Item的position。
                int menuPosition = menuBridge.getPosition(); // 菜单在RecyclerView的Item中的Position。
//                final Object bean = mBaseAdapter.getData().get(adapterPosition);

                if (direction == SwipeMenuRecyclerView.RIGHT_DIRECTION) {
                    final int position = adapterPosition;
                    int viewtype = mBaseAdapter.getItemViewType(position);
                    //0:二手机 1:设备出租 2:设备求租 3:工程师
                    HashMap params = new HashMap();
                    params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
                    switch (viewtype) {
                        case VIEW_TYPE_USED:
                            MyPublishNewVO.TwoHardListBean bean = (MyPublishNewVO.TwoHardListBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean.getIdX());
                            params.put("isDel", 1);
                            Call<ApiResponseBody> call = mApiservice.deleteUsedMachine(params);
                            call.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });

                            break;
                        case VIEW_TYPE_RENT:
                            MyPublishNewVO.LeaseListBean bean1 = (MyPublishNewVO.LeaseListBean) mBaseAdapter.getData().get(position);
                            params.put("id", bean1.getIdX());
                            params.put("isDel", 1);
                            Call<ApiResponseBody> call1 = mApiservice.deleteRent(params);
                            call1.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case VIEW_TYPE_HOME_SERVICE:
                            MyPublishNewVO.ServiceListBean bean3 = (MyPublishNewVO.ServiceListBean) mBaseAdapter.getData().get(position);
                            params.put("serviceId", bean3.getIdX());
                            params.put("userId", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
                            Call<ApiResponseBody> call3 = mApiservice.delServiceList(params);
                            call3.enqueue(new Callback<ApiResponseBody>() {
                                @Override
                                public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                    if (response.isSuccessful()) {
                                        mBaseAdapter.removeData(position);
                                    } else {
                                        showTost(response.raw().message(), 1);
                                    }
                                }

                                @Override
                                public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                    ApiException apiException = ExceptionHandle.handleException(t);
                                    if (apiException.isTokenInvalid()) {
                                        showUnLoginSnackbar();
                                    } else if (apiException.isNetConnected()) {
                                        showSetNetworkSnackbar();
                                    } else {
                                        showTost(apiException.getMessage(), 0);
                                    }
                                }
                            });
                            break;
                        case 20:
                        case 21:
                        case 22:
                        case 23:
                            FreeAskSearchVO bean6 = (FreeAskSearchVO) mBaseAdapter.getData().get(position);
                            //0:未解答 1:已解答
                            if (bean6.getAnswerList().size()==0) {
//                        mBaseAdapter.removeData(position);
                                params.put("id", bean6.getId());
                                params.put("isDel", 1);
                                Call<ApiResponseBody> call6 = mApiservice.deleteAsk(params);
                                call6.enqueue(new Callback<ApiResponseBody>() {
                                    @Override
                                    public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                                        if (response.isSuccessful()) {
                                            mBaseAdapter.removeData(position);
                                        } else {
                                            showTost(response.raw().message(), 1);
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                                        ApiException apiException = ExceptionHandle.handleException(t);
                                        if (apiException.isTokenInvalid()) {
                                            showUnLoginSnackbar();
                                        } else if (apiException.isNetConnected()) {
                                            showSetNetworkSnackbar();
                                        } else {
                                            showTost(apiException.getMessage(), 0);
                                        }
                                    }
                                });
                            } else {
                                showTost("已经回答的问题不能删除", 0);
                            }
                            break;

                    }
                } else if (direction == SwipeMenuRecyclerView.LEFT_DIRECTION) {
                }
            }
        };
        mBaseAdapter = new BaseAdapter(getActivity()) {
            @Override
            public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
                BaseViewHolder holder = null;
                //二手机
                if (viewType == VIEW_TYPE_USED) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_used_machine, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof  MyPublishNewVO.TwoHardListBean) {
                                        MyPublishNewVO.TwoHardListBean bean = (MyPublishNewVO.TwoHardListBean) data;
                                        ImageView saleState = holder.getView(R.id.iv_saled);
                                        ImageView img = holder.getView(R.id.iv_machine);
                                        ImageDisplayUtil.LoadNetImageWithUrl(img, getActivity(), bean.getBanner_pic());
                                        holder.setText(R.id.tv_used_machine_name, bean.getTitle());
                                        holder.setText(R.id.detail, Utils.getDateYear(bean.getProduction_date()) +
                                                " | " + Utils.D2I(bean.getWorking_hours()) + "小时 | " + bean.getPro_name().replaceAll("省", "") + " " + bean.getCity_name().replaceAll("市", ""));
                                        DecimalFormat df = new DecimalFormat("0.00");
                                        holder.setText(R.id.tv_price, df.format(bean.getPrice()));
//                                    holder.setText(R.id.tvUnit, bean.getPrice_unit());
                                        Date date = Util.StrToDate(bean.getAdd_time(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);
                                        holder.setText(R.id.tvUnit, "万");
                                        if (bean.getState() == 0) {
                                            saleState.setVisibility(View.GONE);
                                        } else {
                                            saleState.setVisibility(View.VISIBLE);
                                        }

                                    }

                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    MyPublishNewVO.TwoHardListBean bean = (MyPublishNewVO.TwoHardListBean) mBaseAdapter.getData().get(position);
//                                    Intent intent = new Intent(getActivity(), UsedMachineDetailActivity.class);
//                                    intent.putExtra("id", bean.getId());
//                                    intent.putExtra("unit", bean.getPrice_unit());
//                                    startActivity(intent);
                                    Intent intent = new Intent(getActivity(), EditUsedMachineActivity.class);
                                    intent.putExtra("deviceid", bean.getIdX());
                                    startActivity(intent);
                                }
                            });
                    //设备出租
                } else if (viewType == VIEW_TYPE_RENT) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_used_machine, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof MyPublishNewVO.LeaseListBean) {
                                        MyPublishNewVO.LeaseListBean bean = (MyPublishNewVO.LeaseListBean) data;
//                                        ImageView saleState = holder.getView(R.id.iv_saled);
                                        ImageView img = holder.getView(R.id.iv_machine);
                                        ImageDisplayUtil.LoadNetImageWithUrl(img, getActivity(), bean.getBanner_pic());
                                        holder.setText(R.id.tv_used_machine_name, bean.getTitle());
                                        holder.setText(R.id.detail, bean.getPro_name().replaceAll("省", "") + " " + bean.getCity_name().replaceAll("市", ""));
//                                    DecimalFormat df = new DecimalFormat("0.00");
                                        holder.setText(R.id.tv_price, Utils.D2I(bean.getPrice()) + "");
//                                    holder.setText(R.id.tvUnit, bean.getPrice_unit());
                                        Date date = Util.StrToDate(bean.getAdd_time(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);
                                        holder.setText(R.id.tvUnit, bean.getPrice_unit());
//                                        if (bean.getState() == 0) {
//                                            holder.setText(R.id.tag, "待租");
//                                        } else {
//                                            holder.setText(R.id.tag, "忙碌");
//                                        }
                                    }

                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    MyPublishNewVO.LeaseListBean bean = (MyPublishNewVO.LeaseListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(getActivity(), EditRentActicity.class);
                                    intent.putExtra("deviceid", bean.getIdX());
                                    startActivity(intent);
                                }
                            });
                } else if (viewType == VIEW_TYPE_HOME_SERVICE) {//上门服务
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_home_service, parent, false),
                            new BaseViewHolder.Callbacks() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof  MyPublishNewVO.ServiceListBean) {
                                        ImageView userHead = holder.getView(R.id.userHead);
                                        MyPublishNewVO.ServiceListBean bean = (MyPublishNewVO.ServiceListBean) data;
                                        holder.setText(R.id.userName, bean.getUserName());
                                        holder.setText(R.id.serviceContent, bean.getServiceInfo());
                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);
                                        String pro = bean.getProviceName().replaceAll("省", "");
                                        String city = bean.getCityName().replaceAll("市", "");
                                        if (TextUtils.isEmpty(pro + " " + city))
                                            holder.setText(R.id.serviceAddress, "未知地区");
                                        else
                                            holder.setText(R.id.serviceAddress,pro+" "+city);

//                                    ImageDisplayUtil.LoadNetImageWithUrl(userHead, getActivity(), bean.getUserPic());
                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(),userHead,  bean.getUserPic());
                                        String type = bean.getType();
                                        String serviceType = "维修";
                                        if(type.equals("1")){
                                            serviceType =("维修");
                                        }else if(type.equals("2")){
                                            serviceType =("检车");
                                        }else if(type.equals("3")){
                                            serviceType =("保养");
                                        }else if(type.equals("4")){
                                            serviceType =("评估");
                                        }
                                        holder.setText(R.id.serviceType,serviceType);

                                    }
                                }

                                @Override
                                public void onItemClick(View view, int position) {
                                    MyPublishNewVO.ServiceListBean bean = (MyPublishNewVO.ServiceListBean) mBaseAdapter.getData().get(position);
                                    Intent intent = new Intent(getActivity(), HomeServiceDetailActivity.class);
                                    intent.putExtra("id", String.valueOf(bean.getIdX()));
//                                intent.putExtra("unit", bean.getPrice_unit());
                                    startActivity(intent);
                                }
                            });

                } else if (viewType == 20) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_hot, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);

                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerList().size() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }
                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });

                } else if (viewType == 21) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img1, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);

                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerList().size() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }

                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });

                } else if (viewType == 22) {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img2, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                                        ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                                        LinearLayout llQuestionImg = holder.getView(R.id.llQuestionImg);
                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        llQuestionImg.setVisibility(View.VISIBLE);
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg2, getActivity(), bean.getImgList().get(1));

                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerList().size() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }

                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });
                } else {
                    holder = new BaseViewHolder(LayoutInflater.from(getActivity()).inflate(R.layout.item_free_ask_img3, parent, false),
                            new BaseViewHolder.Callbacks2() {
                                @Override
                                public void bind(BaseViewHolder holder, Object data) {
                                    if (data instanceof FreeAskSearchVO) {
                                        final FreeAskSearchVO bean = (FreeAskSearchVO) data;


                                        LinearLayout allAnswer = holder.getView(R.id.llAnswerComment);
                                        ImageView askHead = holder.getView(R.id.iv_head_ask);
//                                    ImageView answerHead = holder.getView(R.id.iv_answer_head);
                                        ImageView ivZan = holder.getView(R.id.ivZan);
                                        ImageView ivQuestionImg1 = holder.getView(R.id.ivQuestionImg1);
                                        ImageView ivQuestionImg2 = holder.getView(R.id.ivQuestionImg2);
                                        ImageView ivQuestionImg3 = holder.getView(R.id.ivQuestionImg3);
                                        LinearLayout llQuestionImg = holder.getView(R.id.llQuestionImg);
                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), askHead, bean.getUserPic());
                                        holder.setText(R.id.tvAskName, bean.getUsername());
                                        holder.setText(R.id.tvZan, bean.getProblemLikeNum());
                                        holder.setText(R.id.tv_answer, bean.getContent());

                                        llQuestionImg.setVisibility(View.VISIBLE);

                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg1, getActivity(), bean.getImgList().get(0));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg2, getActivity(), bean.getImgList().get(1));
                                        ImageDisplayUtil.LoadASKIMG(ivQuestionImg3, getActivity(), bean.getImgList().get(2));


                                        Date date = Util.StrToDate(bean.getAddTime(), "yyyy.MM.dd HH:mm:ss");
                                        String timeStr = Utils.getAskTime(date);
                                        holder.setText(R.id.addTime, timeStr);

                                        if (bean.getAnswerList().size() > 0) {
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.VISIBLE);
                                            holder.setText(R.id.answerNumber, bean.getAnswerList().size() + " 回复");
//                                        ImageDisplayUtil.loadCircleHeadImage(getActivity(), answerHead, bean.getUserPic());
                                            holder.setText(R.id.tvAnswerName, bean.getAnswerList().get(0).getUsername1());
                                            holder.setText(R.id.tvAnswerContent, bean.getAnswerList().get(0).getContent());
                                            //判断是否工程师
                                            if (bean.getAnswerList().get(0).getIsProjector().equals("1")) {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.VISIBLE);
                                            } else {
                                                holder.getView(R.id.tvCommentType).setVisibility(View.INVISIBLE);
                                            }
                                            if (bean.getAnswerList().size() > 1) {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.VISIBLE);
                                                holder.setText(R.id.tvAnswerName2, bean.getAnswerList().get(1).getUsername1());
                                                holder.setText(R.id.tvAnswerContent2, bean.getAnswerList().get(1).getContent());
                                                //判断是否工程师
                                                if (bean.getAnswerList().get(1).getIsProjector().equals("1")) {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.VISIBLE);
                                                } else {
                                                    holder.getView(R.id.tvCommentType2).setVisibility(View.INVISIBLE);
                                                }
                                            } else {
                                                holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                            }

                                        } else {
                                            holder.setText(R.id.answerNumber, "无回复");
                                            holder.getView(R.id.llAnswerComment).setVisibility(View.GONE);
                                            holder.getView(R.id.llAnswer2).setVisibility(View.GONE);
                                        }
                                        allAnswer.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {
                                                FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( bean.getId(),getContext());
                                            }
                                        });
                                        if (TextUtils.isEmpty(bean.getLikeId()))
                                            ivZan.setImageResource(R.mipmap.zan_2);
                                        else {
                                            ivZan.setImageResource(R.mipmap.zan_1);
                                        }

                                    }
                                }

                                @Override
                                public void onItemClick(BaseViewHolder holder, View view, int position) {
                                    FreeAskSearchVO b = (FreeAskSearchVO) mBaseAdapter.getData(position);
                                    FreeAskAnswerDetailActivity.startFreeAskAnswerDetailActivty( b.getId(),getContext());
                                }
                            });
                }
                return holder;
            }


            @Override
            public int getItemViewType(int position) {
                Object bean = mBaseAdapter.getData().get(position);
                if (bean instanceof MyPublishNewVO.LeaseListBean) {
                    return VIEW_TYPE_RENT;
                } else if (bean instanceof MyPublishNewVO.TwoHardListBean) {
                    return VIEW_TYPE_USED;
                } else if (bean instanceof FreeAskSearchVO) {
                    FreeAskSearchVO b = (FreeAskSearchVO) bean;
                    if (b.getImgList().size() == 1)
                        return 21;
                    if (b.getImgList().size() == 2)
                        return 22;
                    if (b.getImgList().size() == 3)
                        return 23;
                    if (b.getImgList().size() == 0)
                        return 20;
                    return 20;
                } else if (bean instanceof MyPublishNewVO.ServiceListBean) {
                    return VIEW_TYPE_HOME_SERVICE;
                } else
                    return super.getItemViewType(position);
            }
        };

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setSwipeMenuCreator(swipeMenuCreator);
        mRecyclerView.setSwipeMenuItemClickListener(mMenuItemClickListener);
        mRecyclerView.setAdapter(mBaseAdapter);


    }

    @Override
    public void loadData() {
        getData();

    }


    public void getData() {
        mApiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("type", type);
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
        Call<ApiResponseBody<MyPublishNewVO>> call = mApiservice.mySubList(params);
        call.enqueue(new Callback<ApiResponseBody<MyPublishNewVO>>() {
            @Override
            public void onResponse(Call<ApiResponseBody<MyPublishNewVO>> call, Response<ApiResponseBody<MyPublishNewVO>> response) {
                if (response.isSuccessful()) {
                    MyPublishNewVO bean = response.body().getResult();
                    if (bean == null) {
                        visibleNoData();
                    } else {
                        mBaseAdapter.removeAllData();

                        if (bean.getLeaseList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getLeaseList());
                        if (bean.getTwoHardList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getTwoHardList());
                        if (bean.getServiceList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getServiceList());
                        if (bean.getNwlProblemList() != null)
                            mBaseAdapter.addData(BaseAdapter.AddType.LASE, bean.getNwlProblemList());
                    }
                    if (mBaseAdapter.getData().size() == 0) {
                        visibleNoData();
                    }
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody<MyPublishNewVO>> call, Throwable t) {
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 0);
                }
            }
        });


    }


    private OnItemMovementListener mItemMovementListener = new OnItemMovementListener() {
        @Override
        public int onDragFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            return 0;
        }

        @Override
        public int onSwipeFlags(RecyclerView recyclerView, RecyclerView.ViewHolder targetViewHolder) {
            int adapterPosition = targetViewHolder.getAdapterPosition();
            int viewtype = mBaseAdapter.getItemViewType(adapterPosition);
            if (viewtype == VIEW_TYPE_ASK) {
                MyPublishVO.ProblemListBean bean = (MyPublishVO.ProblemListBean) mBaseAdapter.getData().get(adapterPosition);
                if (bean.getIsAnswer() == 1) {
                    return OnItemMovementListener.INVALID;// 返回无效的方向。
                }
            }
            return OnItemMovementListener.LEFT | OnItemMovementListener.RIGHT;
        }
    };

    /**
     * 当Item移动的时候监听。
     */
    private OnItemMoveListener onItemMoveListener = new OnItemMoveListener() {
        @Override
        public boolean onItemMove(RecyclerView.ViewHolder srcHolder, RecyclerView.ViewHolder targetHolder) {
            return false;
        }


        @Override
        public void onItemDismiss(RecyclerView.ViewHolder srcHolder) {
            final int position = srcHolder.getAdapterPosition();
            int viewtype = mBaseAdapter.getItemViewType(position);

        }
    };


    @Override
    public void onResume() {
        super.onResume();
        getData();
    }
}
